﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Data;
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Base.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Layout;
using DevExpress.XtraGrid.Views.Layout.ViewInfo;
using WCFMINERP.ComControls.Extension;

namespace WCFMINERP.ComControls
{
	public class GridControlAdapter {
		private readonly GridControl _gdc;
		private readonly Form form;
		private Func<object, string> GetFilePath;
		//    private Action<object, bool> SelectRowAction;
		private RepositoryItemCheckEdit _CheckEdit;
		protected ArrayList _Selection;
		private BaseHitInfo _downHitInfo;
		private bool _selfDrop;
		public GridColumn colSelected { get; set; }
		private bool cursorOutWin;
		public GridControlAdapter(GridControl gcControl) {
			_gdc = gcControl;
			form = _gdc.FindFormInternal();
		}
		private bool DragedMoveoutItem { get; set; }
		private bool DragedAddItem { get; set; }
		public int SelectedCount {
			get { return _Selection.Count; }
		}
		public event CollectionChangedEventHandler SelectionCollectionChanged;
		public event CollectionChangedEventHandler SelectionCollectionChanging;
		protected virtual void OnSelectionCollectionChanging(CollectionChangedEventArgs args) {
			CollectionChangedEventHandler handler = SelectionCollectionChanging;
			if (handler != null) {
				handler(this, args);
			}
		}
		protected virtual void OnSelectionCollectionChanged(CollectionChangedEventArgs args) {
			CollectionChangedEventHandler handler = SelectionCollectionChanged;
			if (handler != null) {
				handler(this, args);
			}
		}
        //public void ClearSelection()
        //{
        //    _Selection = new ArrayList();
        //}
		public void EnableMouseDownMoveSelectRow(GridView view) {
			view.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
//			view.MouseMove += view_MouseMove;
//			_gdc.MouseMove += _gdc_MouseMove;
		}
		private void _gdc_MouseMove(object sender, MouseEventArgs e) {
			GridView baseView = sender as GridView;
			if (e.Button.HasLeft()) {
				GridHitInfo calcHitInfo = baseView.CalcHitInfo(e.Location);
				if (calcHitInfo.RowHandle > -1) {
					baseView.SelectRow(calcHitInfo.RowHandle);
				}
			}
		}
		private void view_MouseMove(object sender, MouseEventArgs e) {
			GridControl gridControl = sender as GridControl;
			GridView baseView = gridControl.FocusedView as GridView;
			if (e.Button.HasLeft()) {
				GridHitInfo calcHitInfo = baseView.CalcHitInfo(e.Location);
				if (calcHitInfo.RowHandle > -1) {
					baseView.SelectRow(calcHitInfo.RowHandle);
				}
			}
		}
		/// <summary>
		/// </summary>
		/// <param name="gridView">启用拖拽的gridview</param>
		/// <param name="moveoutItem">以后可已出项</param>
		/// <param name="addItem">是否可向当前gridview拖入项</param>
		/// <param name="selfDrop">是否再拖出后移除原有项</param>
		/// <param name="getFilePath"></param>
		public void EnableDrag(ColumnView gridView, bool moveoutItem = true, bool addItem = true, bool selfDrop = false,
								Func<object, string> getFilePath = null) {
			_gdc.AllowDrop = true;
			DragedMoveoutItem = moveoutItem;
			DragedAddItem = addItem;
			form.AllowDrop = true;
			_selfDrop = selfDrop;
			GetFilePath = getFilePath;
			_gdc.DragOver -= GridControlEx_DragOver;
			_gdc.DragDrop -= GridControlEx_DragDrop;
			_gdc.DragOver += GridControlEx_DragOver;
			_gdc.DragDrop += GridControlEx_DragDrop;
			gridView.MouseDown += MainView_MouseDown;
			gridView.MouseMove += MainView_MouseMove;
			//      ApplicationMessageFilter.MouseFilter.NotifyMouseEvent += (sender, args) => {
			////         MouseMessageEvent mme = args as MouseMessageEvent;
			////         if (mme == null) return;
			////         bool r = mme.MouseInForm;
			//      };
		}
		public void DoubleClickRowExpand(GridView gv) {
			gv.DoubleClick -= gv_DoubleClick;
			gv.DoubleClick += gv_DoubleClick;
			if (_gdc.FocusedView is GridView) {
				_gdc.PreviewKeyDown += GdcOnPreviewKeyDown;
			}
		}
		private void GdcOnPreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
			GridView gv = _gdc.FocusedView as GridView;
			if (gv == null) {
				return;
			}
			GridHitInfo calcHitInfo = gv.CalcHitInfo(gv.GridControl.PointToClient(Cursor.Position));
			if (!calcHitInfo.InRowCell) {
				return;
			}
			if (e.KeyCode == Keys.Right) {
				gv.SetMasterRowExpanded(gv.FocusedRowHandle, true);
			}
			if (e.KeyCode == Keys.Left) {
				if (_gdc.FocusedView.IsDetailView) {
					GridView gridView = _gdc.FocusedView as GridView;
					GridView view = gridView.ParentView as GridView;
					view.SetMasterRowExpanded(view.FocusedRowHandle, false);
					gridView.ExpandAllGroups();
				}
				else {
					gv.SetMasterRowExpanded(gv.FocusedRowHandle, false);
					gv.ExpandAllGroups();
				}
			}
		}
		private void gv_DoubleClick(object sender, EventArgs e) {
			try {
				GridView gv = sender as GridView;
				if (gv != null && gv.OptionsView.ShowDetailButtons) {
					gv.SetMasterRowExpanded(gv.FocusedRowHandle, !gv.GetMasterRowExpanded(gv.FocusedRowHandle));
				}
			}
			catch (Exception) {
				throw;
			}
		}
		private void MainView_MouseMove(object sender, MouseEventArgs e) {
			ColumnView view = sender as ColumnView;
			if (_downHitInfo != null) {
				Size dragSize = SystemInformation.DragSize;
				Rectangle dragRect = new Rectangle(new Point(_downHitInfo.HitPoint.X - dragSize.Width/2, _downHitInfo.HitPoint.Y - dragSize.Height/2),
													dragSize);
				if (!dragRect.Contains(new Point(e.X, e.Y))) {
					//            Console.WriteLine(e.X + "_" + e.Y);
					DropData row = getDragData(view, _downHitInfo);
					if (GetFilePath != null) {
						List<string> files = row.Data.Cast<object>().Select(o => GetFilePath(o)).Where(filePath => filePath != null).ToList();
						if (files.Any()) {
							DataObject data = new DataObject(DataFormats.FileDrop, files.ToArray());
							data.SetData(DataFormats.StringFormat, files[0]);
							view.GridControl.DoDragDrop(data, DragDropEffects.Copy);
						}
					}
					else {
						view.GridControl.DoDragDrop(row, DragDropEffects.All);
					}
					_downHitInfo = null;
					DXMouseEventArgs.GetMouseArgs(e).Handled = true;
				}
			}
		}
		private DropData getDragData(ColumnView view, BaseHitInfo hitInfo) {
			int[] selection = view.GetSelectedRows();
			if (selection == null) {
				return null;
			}
			int count = selection.Length;
			DropData data = new DropData();
			data.Control = _gdc;
			data.View = view;
			List<object> items = new List<object>();
			data.Data = items;
			for (int i = 0; i < count; i++) {
				items.Add(view.GetRow(selection[i]));
			}
			if (hitInfo is GridHitInfo) {
				GridHitInfo gdhitInfo = hitInfo as GridHitInfo;
				if (gdhitInfo.InRow) {
					object t = view.GetRow(gdhitInfo.RowHandle);
					if (!items.Contains(t)) {
						items.Add(t);
					}
				}
			}
			if (items.Count > 0) {
				data.FirstData = items[0];
			}
			return data;
		}
        
		private void MainView_MouseDown(object sender, MouseEventArgs e) {
			ColumnView view = sender as ColumnView;
			if (view is GridView) {
				GridView gdv = view as GridView;
				GridHitInfo gdvhit = gdv.CalcHitInfo(new Point(e.X, e.Y));
				if (!gdvhit.InRow || gdvhit.InFilterPanel) {
					return;
				}
			}
			else if (view is LayoutView) {
				LayoutView lyt = view as LayoutView;
				if (!lyt.CalcHitInfo(new Point(e.X, e.Y)).InCard) {
					return;
				}
			}
			if (view == null) {
				return;
			}
			_downHitInfo = null;
			BaseHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
			if (Control.ModifierKeys != Keys.None) {
				return;
			}
			if (e.Button == MouseButtons.Left) { 
				_downHitInfo = hitInfo;
			}
		}
		private void GridControlEx_DragOver(object sender, DragEventArgs e) {
			//        e.Effect = DragDropEffects.Copy;
			if (e.Data.GetDataPresent(typeof (DropData)) || e.Data.GetFormats().Any(c => c == DataFormats.FileDrop)) {
				e.Effect = DragDropEffects.Copy;
			}
			else {
				e.Effect = DragDropEffects.None;
			}
		}
		private void onDataDroping(DataDropingEventArgs args) {
			DataDropingHandler handler = DataDroping;
			if (handler != null) {
				handler(_gdc, args);
			}
		}
		public event DataDropingHandler DataDroping;
		public event DataDropedHandler DataDroped;
		private void onDataDroped(DataDropedEventArgs args) {
			DataDropedHandler handler = DataDroped;
			if (handler != null) {
				handler(_gdc, args);
			}
		}
		private void GridControlEx_DragDrop(object sender, DragEventArgs e) {
			GridControl grid = sender as GridControl;
			if (grid == null) {
				return;
			}
			Point point = new Point(e.X, e.Y);
			BaseView baseView = grid.GetViewAt(point);
			if (baseView == null) {
				baseView = grid.FocusedView;
			}
			BaseHitInfo calcHitInfo = baseView.CalcHitInfo(grid.PointToClient(point));
			DropData dropData = e.Data.GetData(typeof (DropData)) as DropData;
			if (dropData != null) {
				if (dropData.Control == _gdc && !_selfDrop) {
					return;
				}
				DataDropingEventArgs args = new DataDropingEventArgs {Data = dropData.Data, HitInfo = calcHitInfo, DataType = DropDataType.Object};
				onDataDroping(args);
				if (!args.Cancel) {
					DataDropedEventArgs argsDroped = new DataDropedEventArgs {Data = dropData.Data, HitInfo = calcHitInfo, DataType = DropDataType.Object};
					onDataDroped(argsDroped);
				}

				//				foreach (object value in dropData.Data) {
				//					DataDropingEventArgs args = new DataDropingEventArgs {Data = value, HitInfo = calcHitInfo, DataType = DropDataType.Object};
				//					onDataDroping(args);
				//					if (!args.Cancel) {
				//						DataDropedEventArgs argsDroped = new DataDropedEventArgs {Data = args.Data, HitInfo = calcHitInfo, DataType = DropDataType.Object};
				//						onDataDroped(argsDroped);
				//					}
				//				}
			}
			else if (e.Data.GetFormats().Any(c => c == DataFormats.FileDrop)) {
				dropData = new DropData();
				string[] strings = e.Data.GetData(DataFormats.FileDrop) as string[];
				if (strings != null) {
					dropData.Data = strings;
					if (strings.Length > 0) {
						dropData.FirstData = strings[0];
					}
					foreach(object value in dropData.Data) {
						DataDropingEventArgs args = new DataDropingEventArgs {Data = value, HitInfo = calcHitInfo, DataType = DropDataType.Path};
						onDataDroping(args);
						if (!args.Cancel) {
							DataDropedEventArgs argsDroped = new DataDropedEventArgs {Data = args.Data, HitInfo = calcHitInfo, DataType = DropDataType.Path};
							onDataDroped(argsDroped);
						}
					}
				}
			}
		}
		public void SetPopupMenu<T>(BaseView baseView, PopupMenu popupMenu, Func<T, bool> canShow) where T : class {
			_gdc.MouseUp += (sender, e) => {
				BaseHitInfo hi = baseView.CalcHitInfo(e.Location);
				if (hi is GridHitInfo) {
					GridHitInfo gridHitInfo = hi as GridHitInfo;
					T data = baseView.GetRow(gridHitInfo.RowHandle) as T;
					if (e.Button == MouseButtons.Right && canShow(data)) {
						popupMenu.ShowPopup(Cursor.Position);
					}
				}
				if (hi is LayoutViewHitInfo) {
					LayoutViewHitInfo layoutViewHitInfo = hi as LayoutViewHitInfo;
					T data = baseView.GetRow(layoutViewHitInfo.RowHandle) as T;
					if (e.Button == MouseButtons.Right && canShow(data)) {
						popupMenu.ShowPopup(Cursor.Position);
					}
				}
			};
		}
		public GridColumn ShowCheckColumn(GridView gview, bool clickOnRow = false) {
			ClickOnRow = clickOnRow;
			_Selection = new ArrayList();
			_CheckEdit = (RepositoryItemCheckEdit) gview.GridControl.RepositoryItems.Add("CheckEdit");
			_CheckEdit.EditValueChanged += (sender, args) => {
				CheckEdit checkEdit = sender as CheckEdit;
				SelectRow(gview, gview.FocusedRowHandle, checkEdit.Checked, true);
				gview.PostEditor();
			};
			_CheckEdit.EditValueChanging += (sender, args) => {
				CheckEdit checkEdit = sender as CheckEdit;
				if (checkEdit != null) {
					args.Cancel = SelectingRow(gview, gview.FocusedRowHandle, (bool) args.NewValue, true);
				}
			};
			colSelected = gview.Columns.Insert(0);
			colSelected.FieldName = "colSelected";
			colSelected.Name = "colSelected";
			colSelected.OptionsColumn.AllowIncrementalSearch = false;
			colSelected.OptionsColumn.AllowMove = false;
			colSelected.OptionsColumn.AllowShowHide = false;
			colSelected.OptionsColumn.AllowSize = false;
			colSelected.OptionsColumn.FixedWidth = true;
			colSelected.OptionsColumn.ShowCaption = false;
			colSelected.Visible = true;
			colSelected.VisibleIndex = 0;
			colSelected.Width = 41;
			colSelected.ColumnEdit = _CheckEdit;
			colSelected.UnboundType = UnboundColumnType.Boolean;
			gview.CustomUnboundColumnData += gvOrderInfo_CustomUnboundColumnData;
			if (!gview.OptionsBehavior.Editable) {
				gview.OptionsBehavior.Editable = true;
				foreach(GridColumn column in gview.Columns) {
					if (column == colSelected) {
						continue;
					}
					column.OptionsColumn.AllowEdit = false;
					column.OptionsColumn.ReadOnly = true;
				}
			}
			gview.Click += View_Click;
			gview.CustomDrawColumnHeader += gvOrderInfo_CustomDrawColumnHeader;
			gview.CustomDrawGroupRow += View_CustomDrawGroupRow;
			return colSelected;
		}
		private void gvOrderInfo_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) {
			if (e.Column != colSelected) {
				return;
			}
			if (e.IsGetData) {
				e.Value = _Selection.IndexOf(e.Row) > -1;
			}
			if (e.IsSetData) {
				//            bool value = (bool) e.Value;
				//            if (SelectRowAction != null) SelectRowAction(e.Row, value);
				//            SelectRow(e.Column.View as GridView, e.Column.View.GetRowHandle(e.ListSourceRowIndex), value, true);
			}
		}
		private void View_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e) {
			GridGroupRowInfo info = (GridGroupRowInfo) e.Info;
			info.GroupText = "    " + info.GroupText.TrimStart();
			e.Info.Paint.FillRectangle(e.Graphics, e.Appearance.GetBackBrush(e.Cache), e.Bounds);
			e.Painter.DrawObject(e.Info);
			Rectangle r = info.ButtonBounds;
			r.Offset(r.Width*1, -5);
			int g = GroupRowSelectionStatus(sender as GridView, e.RowHandle);
			DrawCheckBox(e.Graphics, r, g > 0, g < 0);
			e.Handled = true;
		}
		public bool IsRowSelected(GridView View, int rowHandle) {
			if (View.IsGroupRow(rowHandle)) {
				return IsGroupRowSelected(View, rowHandle);
			}
			object row = View.GetRow(rowHandle);
			return GetSelectedIndex(row) != -1;
		}
		public int GetSelectedIndex(object row) {
			return _Selection.IndexOf(row);
		}
		public bool IsGroupRowSelected(GridView View, int rowHandle) {
			for (int i = 0; i <= (View.GetChildRowCount(rowHandle)) - 1; i++) {
				int row = View.GetChildRowHandle(rowHandle, i);
				if (View.IsGroupRow(row)) {
					if (!IsGroupRowSelected(View, row)) {
						return false;
					}
				}
				else if (!IsRowSelected(View, row)) {
					return false;
				}
			}
			return true;
		}
		public int GroupRowSelectionStatus(GridView View, int rowHandle) {
			int count = 0;
			for (int i = 0; i <= (View.GetChildRowCount(rowHandle)) - 1; i++) {
				int row = View.GetChildRowHandle(rowHandle, i);
				if (View.IsGroupRow(row)) {
					int g = GroupRowSelectionStatus(View, row);
					if (g < 0) {
						return g;
					}
					if (g > 0) {
						count += 1;
					}
				}
				else if (IsRowSelected(View, row)) {
					count += 1;
				}
			}
			if (count == 0) {
				return 0;
			}
			if (count == View.GetChildRowCount(rowHandle)) {
				return 1;
			}
			return -1;
		}
		private void gvOrderInfo_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e) {
			if (ReferenceEquals(e.Column, colSelected)) {
				GridView View = sender as GridView;
				if (View == null) {
					return;
				}
				e.Info.InnerElements.Clear();
				e.Painter.DrawObject(e.Info);
				int inviewSelectCount = 0;
				for (int i = 0; i < View.DataRowCount; i++) {
					object row = View.GetRow(i);
					if (_Selection.IndexOf(row) > -1) {
						inviewSelectCount++;
					}
				}
				bool gray = inviewSelectCount > 0 & inviewSelectCount < View.DataRowCount;
				DrawCheckBox(e.Graphics, e.Bounds, inviewSelectCount > 0 && inviewSelectCount == View.DataRowCount, gray);
				e.Handled = true;
			}
		}
		public object GetSelectedRow(int index) {
			return _Selection[index];
		}
		public ArrayList GetSelectedRows() {
			return _Selection;
		}
		public bool ClickOnRow { get; set; }
		private void View_Click(object sender, EventArgs e) {
			GridView View = sender as GridView;
			Point pt = View.GridControl.PointToClient(Control.MousePosition);
			GridHitInfo info = View.CalcHitInfo(pt);
			if (info.InColumn & ReferenceEquals(info.Column, colSelected)) {
				if (SelectedCount == View.DataRowCount) {
					ClearSelection(View);
				}
				else {
					SelectAll(View);
				}
			}
			else if (info.InRow & View.IsGroupRow(info.RowHandle) & info.HitTest != GridHitTest.RowGroupButton) {
				bool selected = IsGroupRowSelected(View, info.RowHandle);
				SelectGroup(View, info.RowHandle, !selected);
			}
			else if (info.InRow && ClickOnRow) {
				if (View.OptionsSelection.MultiSelect) {
					int[] selectedRows = View.GetSelectedRows();
					bool selected = IsRowSelected(View, info.RowHandle);
					foreach(int row in selectedRows) {
						SelectRow(View, row, !selected, false);
					}
					View.RefreshData();
				}
			}
		}
		public void SelectRow(GridView view, object row, bool @select, bool invalidate) {
			if (row == null) {
				return;
			}
			int rowHandle;
			for (rowHandle = 0; rowHandle < view.RowCount; rowHandle++) {
				object dr = view.GetRow(rowHandle);
				if (row == dr) {
					break;
				}
			}
			if (IsRowSelected(view, rowHandle) == @select) {
				return;
			}
			if (@select) {
				CollectionChangedEventArgs collectionChangedEventArgs = new CollectionChangedEventArgs {
																											ChangeType = CollectionChangeType.Add,
																											Data = row,
																											Cannel = false
																										};
				OnSelectionCollectionChanging(collectionChangedEventArgs);
				if (!collectionChangedEventArgs.Cannel) {
					_Selection.Add(row);
					OnSelectionCollectionChanged(collectionChangedEventArgs);
				}
			}
			else {
				CollectionChangedEventArgs collectionChangedEventArgs = new CollectionChangedEventArgs {
																											ChangeType = CollectionChangeType.Remove,
																											Data = row,
																											Cannel = false
																										};
				OnSelectionCollectionChanging(collectionChangedEventArgs);
				_Selection.Remove(row);
				if (!collectionChangedEventArgs.Cannel) {
					_Selection.Add(row);
					OnSelectionCollectionChanged(collectionChangedEventArgs);
				}
			}
			if (invalidate) {
				Invalidate(view);
			}
		}
		public void InitSelectRow<TSource, TC>(GridView view, List<TC> rows, Func<TSource, TC, bool> funcEq, Func<TSource, TC, bool> funcC)
			where TSource : class {
			if (rows == null) {
				return;
			}
			ClearSelection();
			for (int rowHandle = 0; rowHandle < view.RowCount; rowHandle++) {
				TSource ds = view.GetRow(rowHandle) as TSource;
				TC r = rows.FirstOrDefault(c => funcEq(ds, c) && funcC(ds, c));
				if (r != null) {
					CollectionChangedEventArgs collectionChangedEventArgs = new CollectionChangedEventArgs {
																												ChangeType = CollectionChangeType.Add,
																												Data = ds,
																												Cannel = false
																											};
					collectionChangedEventArgs.ChangeType = CollectionChangeType.Add;
					_Selection.Add(ds);
				}
			}
			Invalidate(view);
		}
		public bool SelectingRow(GridView view, int rowHandle, bool @select, bool invalidate) {
			if (rowHandle < 0) {
				return false;
			}
			object row = view.GetRow(rowHandle);
			CollectionChangedEventArgs collectionChangedEventArgs = new CollectionChangedEventArgs {Data = row, Cannel = false};
			if (@select) {
				collectionChangedEventArgs.ChangeType = CollectionChangeType.Add;
				OnSelectionCollectionChanging(collectionChangedEventArgs);
			}
			else {
				collectionChangedEventArgs.ChangeType = CollectionChangeType.Remove;
				OnSelectionCollectionChanging(collectionChangedEventArgs);
			}
			return collectionChangedEventArgs.Cannel;
		}
		public void SelectRow(GridView view, int rowHandle, bool @select, bool invalidate) {
			if (rowHandle < 0) {
				return;
			}
			object row = view.GetRow(rowHandle);
			if (IsRowSelected(view, rowHandle) == @select) {
				return;
			}
			CollectionChangedEventArgs collectionChangedEventArgs = new CollectionChangedEventArgs {Data = row, Cannel = false};
			if (@select) {
				collectionChangedEventArgs.ChangeType = CollectionChangeType.Add;
				_Selection.Add(row);
				OnSelectionCollectionChanged(collectionChangedEventArgs);
			}
			else {
				collectionChangedEventArgs.ChangeType = CollectionChangeType.Remove;
				_Selection.Remove(row);
				OnSelectionCollectionChanged(collectionChangedEventArgs);
			}
			if (invalidate) {
				Invalidate(view);
			}
		}
		public void SelectGroup(GridView View, int rowHandle, bool @select) {
			if (IsGroupRowSelected(View, rowHandle) & @select) {
				return;
			}
			for (int i = 0; i <= (View.GetChildRowCount(rowHandle)) - 1; i++) {
				int childRowHandle = View.GetChildRowHandle(rowHandle, i);
				if (View.IsGroupRow(childRowHandle)) {
					SelectGroup(View, childRowHandle, @select);
				}
				else {
					SelectRow(View, childRowHandle, @select, false);
				}
			}
			Invalidate(View);
		}
		public void SelectAll(GridView View) {
			ClearSelection(View);
			for (int i = 0; i <= View.DataRowCount - 1; i++) {
				SelectRow(View, i, true, false);
			}
			Invalidate(View);
		}
		public void SelectReverse(GridView view) {
			//ClearSelection(View);
			for (int rowHandle = 0; rowHandle <= view.DataRowCount - 1; rowHandle++) {
				object row = view.GetRow(rowHandle);
				if (IsRowSelected(view, rowHandle)) {
					SelectRow(view, rowHandle, false, false);
				}
				else {
					SelectRow(view, rowHandle, true, false);
				}
			}
			Invalidate(view);
		}
		public void ClearSelection(GridView View) {
			for (int i = 0; i <= View.DataRowCount - 1; i++) {
				SelectRow(View, i, false, false);
			}
			Invalidate(View);
		}
		public void ClearSelection() {
			_Selection.Clear();
		}
		private void Invalidate(ColumnView View) {
			View.RefreshData();
		}
		protected void DrawCheckBox(Graphics g, Rectangle r, bool Checked, bool Grayed) {
			CheckEditViewInfo info = (CheckEditViewInfo) _CheckEdit.CreateViewInfo();
			CheckEditPainter painter = (CheckEditPainter) _CheckEdit.CreatePainter();
			if (Grayed) {
				info.EditValue = _CheckEdit.ValueGrayed;
			}
			else {
				info.EditValue = Checked;
			}
			info.Bounds = r;
			info.CalcViewInfo(g);
			ControlGraphicsInfoArgs args = new ControlGraphicsInfoArgs(info, new GraphicsCache(g), r);
			painter.Draw(args);
			args.Cache.Dispose();
		}
	}
}
